﻿using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.XSSF.Model;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Text;

namespace Yz.Core.Tools
{
    public static class ExcelToHtmlUtils
    {
        public static string GetHexColor(IWorkbook workbook, short colorNum)
        {
            //IL_0026: Unknown result type (might be due to invalid IL or missing references)
            //IL_0087: Unknown result type (might be due to invalid IL or missing references)
            string result = "";
            if (workbook == null)
            {
                return result;
            }
            if (workbook is HSSFWorkbook)
            {
                HSSFPalette customPalette = ((HSSFWorkbook)workbook).GetCustomPalette();
                HSSFColor color = customPalette.GetColor(colorNum);
                if (color != null)
                {
                    result = GetColor(color);
                }
            }
            else
            {
                IndexedColors val = null;
                try
                {
                    val = IndexedColors.ValueOf((int)colorNum);
                }
                catch (Exception)
                {
                    val = IndexedColors.Black;
                }
                if (val != null)
                {
                    result = val.HexString;
                }
                else
                {
                    XSSFColor themeColor = ((XSSFWorkbook)workbook).GetStylesSource().GetTheme().GetThemeColor((int)colorNum);
                    if (themeColor != null)
                    {
                        result = GetColor(themeColor);
                    }
                }
            }
            return result;
        }

        public static string GetHexBgColor(IWorkbook workbook, ICellStyle cellStyle)
        {
            
            string result = "";
            if (workbook == null)
            {
                return result;
            }
            if (workbook is HSSFWorkbook)
            {
                HSSFPalette customPalette = ((HSSFWorkbook)workbook).GetCustomPalette();
                if ((int)cellStyle.FillPattern != 0)
                {
                    if ((int)cellStyle.FillPattern == 1)
                    {
                        HSSFColor color = customPalette.GetColor(cellStyle.FillForegroundColor);
                        if (color != null)
                        {
                            result = GetColor(color);
                        }
                    }
                    else
                    {
                        HSSFColor color2 = customPalette.GetColor(cellStyle.FillBackgroundColor);
                        if (color2 != null)
                        {
                            result = GetColor(color2);
                        }
                    }
                }
            }
            else if ((int)cellStyle.FillPattern != 0)
            {
                if ((int)cellStyle.FillPattern == 1)
                {
                    IndexedColors val = null;
                    try
                    {
                        val = IndexedColors.ValueOf((int)cellStyle.FillForegroundColor);
                    }
                    catch (Exception)
                    {
                    }
                    string text = null;
                    if (val != null)
                    {
                        text = val.HexString;
                    }
                    else
                    {
                        XSSFColor val2 = (XSSFColor)cellStyle.FillForegroundColorColor;
                        if (val2 != null)
                        {
                            text = GetColor(val2);
                        }
                    }
                    if (text != null)
                    {
                        result = text;
                    }
                }
                else
                {
                    IndexedColors val3 = null;
                    try
                    {
                        val3 = IndexedColors.ValueOf((int)cellStyle.FillBackgroundColor);
                    }
                    catch (Exception)
                    {
                    }
                    string text2 = null;
                    if (val3 != null)
                    {
                        text2 = val3.HexString;
                    }
                    else
                    {
                        XSSFColor val4 = (XSSFColor)cellStyle.FillBackgroundColorColor;
                        if (val4 != null)
                        {
                            text2 = GetColor(val4);
                        }
                    }
                    if (text2 != null)
                    {
                        result = text2;
                    }
                }
            }
            return result;
        }

        public static string GetHexFontColor(IWorkbook workbook, IFont font)
        {
            //IL_0016: Unknown result type (might be due to invalid IL or missing references)
            //IL_007f: Unknown result type (might be due to invalid IL or missing references)
            //IL_00bc: Unknown result type (might be due to invalid IL or missing references)
            string result = "";
            if (workbook is HSSFWorkbook)
            {
                HSSFColor color = ((HSSFWorkbook)workbook).GetCustomPalette().GetColor(font.Color);
                if (color != null)
                {
                    result = GetColor(color);
                }
            }
            else
            {
                IndexedColors val = null;
                try
                {
                    val = IndexedColors.ValueOf((int)font.Color);
                }
                catch (Exception)
                {
                    val = IndexedColors.Black;
                }
                string text = null;
                if (val != null)
                {
                    text = val.HexString;
                }
                else
                {
                    StylesTable stylesSource = ((XSSFWorkbook)workbook).GetStylesSource();
                    XSSFColor val2 = (stylesSource == null || stylesSource.GetTheme() == null) ? ((XSSFFont)font).GetXSSFColor() : stylesSource.GetTheme().GetThemeColor((int)font.Color);
                    if (val2 != null)
                    {
                        text = GetColor(val2);
                    }
                }
                if (text != null)
                {
                    result = text;
                }
            }
            return result;
        }

        private static string GetColor(XSSFColor color)
        {
            StringBuilder stringBuilder = new StringBuilder(7);
            stringBuilder.Append('#');
            byte[] rGB = color.RGB;
            byte[] array = rGB;
            for (int i = 0; i < array.Length; i++)
            {
                byte b = array[i];
                stringBuilder.Append(b.ToString("x2"));
            }
            return stringBuilder.ToString();
        }

        private static string GetColor(HSSFColor color)
        {
            StringBuilder stringBuilder = new StringBuilder(7);
            stringBuilder.Append('#');
            byte[] rGB = color.RGB;
            for (int i = 0; i < rGB.Length; i++)
            {
                byte b = rGB[i];
                stringBuilder.Append(b.ToString("x2"));
            }
            return stringBuilder.ToString();
        }
    }

}
